package com.achao.view;

import com.achao.service.AdminUserService;
import com.achao.service.impl.AdminUserServiceImpl;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @Author: achao
 * @DateTime: 2022/7/24 20:04
 * @Description: TODO
 */
@Data
public class AdminLoginView extends JFrame {

    private static AdminLoginView instance;

    public static AdminLoginView getInstance() {
        if (instance == null) {
            instance = new AdminLoginView();
        }
        return instance;
    }

    private JLabel loginLabel = new JLabel("后台登录");
    private JLabel userNameLabel = new JLabel("用户名: ");
    private JLabel userPwdLabel = new JLabel("密码: ");
    private JTextField userNameText = new JTextField();
    private JPasswordField userPwdText = new JPasswordField();
    private JButton loginBtn = new JButton("登录");
    private JButton forgetBtn = new JButton("忘记密码");

    public AdminLoginView() throws HeadlessException {

        setSize(350,250);
        setLayout(null);
        setResizable(false);
        setDefaultCloseOperation(0);
        setLocationRelativeTo(null);

        init();
        action();
    }

    private void init() {
        add(loginLabel);
        loginLabel.setBounds(150,20,100,30);

        add(userNameLabel);
        userNameLabel.setBounds(60,60,60,30);

        add(userPwdLabel);
        userPwdLabel.setBounds(60,90,60,30);

        add(userNameText);
        userNameText.setBounds(120,60,150,30);

        add(userPwdText);
        userPwdText.setBounds(120,90,150,30);

        add(loginBtn);
        loginBtn.setBounds(120,140,100,30);

//        add(forgetBtn);
        forgetBtn.setBounds(125,180,100,20);
        forgetBtn.setVerticalTextPosition(JButton.CENTER);
        forgetBtn.setHorizontalTextPosition(JButton.CENTER);
        forgetBtn.setBorderPainted(false);
        forgetBtn.setContentAreaFilled(false);
    }

    private void action() {
        loginBtn.setActionCommand("login");
        forgetBtn.setActionCommand("forget");
        ActionListener actionListener = e->{
            String command = e.getActionCommand();
            if (command.equals("login")){
                AdminUserService adminUserService = new AdminUserServiceImpl();
                adminUserService.login();
            }  else if (command.equals("forget")) {

            }
        };
        loginBtn.addActionListener(actionListener);
        forgetBtn.addActionListener(actionListener);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int key = JOptionPane.showConfirmDialog(null, "是否关闭窗口?", "提示", JOptionPane.OK_OPTION);
                if (key==0){
                    System.exit(0);
                }
            }
        });
    }
}
